from fastapi import FastAPI
from starlette.middleware.cors import CORSMiddleware
from starlette.staticfiles import StaticFiles
from application.config.fastapi import FastapiConfig, base_router
from application.config.mysql import TORTOISE_ORM
from .router_v1 import v1_router
from tortoise.contrib.fastapi import register_tortoise

app = FastAPI(**FastapiConfig.__dict__)

# 数据库配置
register_tortoise(
    app=app,
    config=TORTOISE_ORM,
)

# 跨域
app.add_middleware(
    CORSMiddleware,
    allow_origins=['*'],
    allow_credentials=True,
    allow_methods=['*'],
    allow_headers=['*'],
)

# 根路由
app.include_router(base_router, tags=["根路径"])
app.include_router(v1_router, prefix='/api')
app.mount('/static', StaticFiles(directory=FastapiConfig.res_path), name='static')
